package de.hsb.ai.agent;

import game.Transport;

import java.util.ArrayList;
import java.util.List;

import de.hsb.ai.domain.GameStarted;
import de.hsb.ai.domain.MoveEvent;
import de.hsb.ai.util.map.ScotlandJadeMapHelper.ScotlandJadeVertex;


/**
 * AbstractPlayer provides common player logic.
 * All devil players should inherit from this class.
 */
@SuppressWarnings("serial")
public abstract class AbstractDetective extends AbstractPlayer {
	
	/**
	 * The abstract service-type.
	 */
	public static final String SERVICE_NAME_PREFIX = "detective";
	
	protected int lastKnownFugitivePosition = -1;
	
	public List<Integer> lastKnownFugitiveTransports = new ArrayList<Integer>(5);
	
	protected final int[] tickets = new int[4];
	
	/**
	 * Creates a new {@link AbstractDetective}.
	 * @param serviceNamePrefix prefix to the service name
	 * @see #SERVICE_TYPE
	 */
	public AbstractDetective(String serviceNamePrefix) {
		super(ScotlandJadeVertex.Type.DETECTIVE, serviceNamePrefix + PREFIX_SEPARATOR + SERVICE_NAME_PREFIX);
	}
	
	@Override
	protected void handleMove(MoveEvent m) {
		super.handleMove(m);
		
		// Player index == 0 indicates a fugitive move
		if (m.getPlayerIndex() == 0) {
			if (m.getMove().getNode() != 0) {
				// Store last known fugitive position
				lastKnownFugitivePosition = m.getMove().getNode();
				lastKnownFugitiveTransports.clear();
			}
			lastKnownFugitiveTransports.add(m.getMove().getType());
		} else if (getAID().equals(m.getPlayer())) {
			--tickets[m.getMove().getType()];
		}
	}
	
	@Override
	protected void handleGameStarted(GameStarted g) {
		super.handleGameStarted(g);
		
		resetTickets();
	}
	
	protected int getTickets(int transport) {
		if (transport < 0 || transport >= tickets.length) {
			throw new IllegalArgumentException("unkown transport: " + transport);
		}
		return tickets[transport];
	}

	private void resetTickets() {
		tickets[Transport.TAXI]	= Transport.DETECTIVE_TAXI_QUOTA;
		tickets[Transport.BUS]	= Transport.DETECTIVE_BUS_QUOTA;
		tickets[Transport.UG]	= Transport.DETECTIVE_UG_QUOTA;
	}
	
}
